//
//  LZLanguageManager.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/22.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZLanguageManager.h"
#import "LZAlertView.h"

@interface LZLanguageManager ()
{
    NSBundle *bundle;
}

@end

@implementation LZLanguageManager

+ (instancetype)shareInstance {
    static LZLanguageManager *lzLanguageManager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lzLanguageManager = [[LZLanguageManager alloc] init];
    });
    
    return lzLanguageManager;
}


-(NSBundle *)getBundle{
    return bundle;
}

#pragma
#pragma mark 语言
- (void)initUserLanguage {
    
    NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
    
    NSString *systemLanguage = [locale.localeIdentifier componentsSeparatedByString:@"_"].firstObject;
    
    NSString *userLanguage = [[NSUserDefaults standardUserDefaults] valueForKey:@"userLanguage"];

    if (userLanguage && ![systemLanguage isEqualToString:userLanguage]) {
        self.isChangeLanguage = YES;
    }
    
    [self setUserlanguage:systemLanguage];
    [self initLanguageBundle:systemLanguage];
}

// 切换语言需要重新启动APP的提示框
-(BOOL)showChangeLanguageAlert {
    if (self.isChangeLanguage) {
        // 系统语言改变了，请重新启动app
        LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"changelanguage_relogin", nil) andImage:nil cancelButtonTitle:nil otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
        
        [alert show];
        
        alert.clickButtonIndexBlock = ^(NSInteger index) {
            exit(0);
        };
        return YES;
    }
    return NO;
}

- (void)initLanguageBundle:(NSString *)language {
    //防御性判断 如果不是阿拉伯就改为英文
    if (![language isEqualToString:@"ar"]) {
        language = @"en";
        [self setAppleLanguages_en];
    }
    else {
        [self setAppleLanguages_ar];
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    
    bundle = [NSBundle bundleWithPath:path];
}

/**
 *  @author taohebin, 16-04-07 13:04:35
 *
 *  设置当前语言
 *
 *  @param language 当前语言
 */
- (void)setUserlanguage:(NSString *)language {
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
    [def setValue:language forKey:@"userLanguage"];
    
    [def synchronize];
}

/**
 *  @author taohebin, 16-04-07 13:04:00
 *
 *  获取当前语言类型
 *
 *  @return 当前用语言类型
 */
- (LanguageType)getLanguageType{
    
    NSString *userLanguage = [[NSUserDefaults standardUserDefaults] valueForKey:@"userLanguage"];
    
    if (userLanguage && [userLanguage isEqualToString:@"ar"]) {
        return LanguageType_Ar;
    }else{
        if (!userLanguage) {
            NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
            if ([locale.localeIdentifier hasPrefix:@"ar"]){
                return LanguageType_Ar;
            }else{
                return LanguageType_En;
            }
        }
        return LanguageType_En;
    }
}

/**
 *  @author taohebin, 16-04-07 13:04:13
 *
 *  设置为当前系统语言为英文
 */
- (void)setAppleLanguages_en {
    
    NSString *currentLanguage = [NSLocale preferredLanguages].firstObject;
    
    currentLanguage = [currentLanguage componentsSeparatedByString:@"-"].firstObject;
    
    if ([currentLanguage isEqualToString:@"en"]) {
        return;
    }
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:@[@"en"] forKey:@"AppleLanguages"];
    [def synchronize];
}

/**
 *  @author taohebin, 16-04-11 12:04:13
 *
 *  设置当前系统语言为阿拉伯文
 */
-(void)setAppleLanguages_ar{
    
    NSString *currentLanguage = [NSLocale preferredLanguages].firstObject;
    
    currentLanguage = [currentLanguage componentsSeparatedByString:@"-"].firstObject;
    
    if ([currentLanguage isEqualToString:@"ar"]) {
        return;
    }
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:@[@"ar"] forKey:@"AppleLanguages"];
    [def synchronize];
}


@end
